/*
JuffEd - A simple text editor
Copyright 2007 Murzin Mikhail

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License 
version 2 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "SettingsDlg.h"

//	Local
#include "main.h"
#include "MultiPage.h"
#include "MainSettings.h"
#include "TextDocSettings.h"

//	Qt
#include <QtCore/QDir>
#include <QtCore/QStringList>
#include <QtGui/QPushButton>
#include <QtGui/QHBoxLayout>
#include <QtGui/QVBoxLayout>

#include "ui_MainSettingsPage.h"

class MainSettingsPage : public QWidget{
public:
	MainSettingsPage() : QWidget() {
		ui.setupUi(this);
	}
	Ui::MainSettingsPage ui;
};

#include "ui_EditorSettingsPage.h"

class EditorSettingsPage : public QWidget {
public:
	EditorSettingsPage() : QWidget () {
		ui.setupUi(this);
	}
	Ui::EditorSettingsPage ui;
};

#include "CharsetsSettingsPage.h"

/////////////////////////////////////////////////////////////

SettingsDlg::SettingsDlg(QWidget* parent) : QDialog(parent) {

	setWindowTitle(tr("Settings"));

	//	create buttons
	okBtn_ = new QPushButton(tr("OK"), this);
	applyBtn_ = new QPushButton(tr("Apply"), this);
	cancelBtn_ = new QPushButton(tr("Cancel"), this);
	connect(okBtn_, SIGNAL(clicked()), SLOT(ok()));
	connect(applyBtn_, SIGNAL(clicked()), SLOT(apply()));
	connect(cancelBtn_, SIGNAL(clicked()), SLOT(reject()));
	//	create multipage
	mp_ = new MultiPage();
	pageMain_ = new MainSettingsPage();
	pageEditor_ = new EditorSettingsPage();
	pageCharsets_ = new CharsetsSettingsPage();
	mp_->addPage(tr("General"), pageMain_);
	mp_->addPage(tr("Editor"), pageEditor_);
	mp_->addPage(tr("Charsets"), pageCharsets_);

	// layouts
	QHBoxLayout* btnLayout = new QHBoxLayout();
	btnLayout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));
	btnLayout->addWidget(okBtn_);
	btnLayout->addWidget(applyBtn_);
	btnLayout->addWidget(cancelBtn_);

	QVBoxLayout* mainLayout = new QVBoxLayout();
	mainLayout->addWidget(mp_);
	mainLayout->addLayout(btnLayout);
	setLayout(mainLayout);
}

SettingsDlg::~SettingsDlg() {
	delete pageCharsets_;
	delete pageEditor_;
	delete pageMain_;
}

void SettingsDlg::init() {
	QDir iconsDir(APPCACHEDIR + "/icons");
	if (iconsDir.exists()) {
		pageMain_->ui.iconThemeCmb->clear();
		pageMain_->ui.iconThemeCmb->addItems(iconsDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot));
	}

	int tabPos = MainSettings::tabPosition();
	QString iconTheme = MainSettings::iconTheme();
	pageMain_->ui.tabPositionCmb->setCurrentIndex(tabPos);
	pageMain_->ui.iconThemeCmb->setCurrentIndex(pageMain_->ui.iconThemeCmb->findText(iconTheme));

	pageEditor_->ui.fontCmb->setCurrentFont(TextDocSettings::font());
	pageEditor_->ui.fontSizeSpin->setValue(TextDocSettings::font().pointSize());
	pageEditor_->ui.widthAdjustChk->setChecked(TextDocSettings::widthAdjust());
	pageEditor_->ui.showLineNumbersChk->setChecked(TextDocSettings::showLineNumbers());
	pageEditor_->ui.tabStopWidthSpin->setValue(TextDocSettings::tabStopWidth());

	pageCharsets_->init();
}

int SettingsDlg::exec() {
	init();
	return QDialog::exec();
}

void SettingsDlg::apply() {
	MainSettings::setTabPosition(pageMain_->ui.tabPositionCmb->currentIndex());
	MainSettings::setIconTheme(pageMain_->ui.iconThemeCmb->currentText());

	QFont font(pageEditor_->ui.fontCmb->currentFont());
	font.setPointSize(pageEditor_->ui.fontSizeSpin->value());
	TextDocSettings::setFont(font);
	TextDocSettings::setWidthAdjust(pageEditor_->ui.widthAdjustChk->isChecked());
	TextDocSettings::setShowLineNumbers(pageEditor_->ui.showLineNumbersChk->isChecked());
	TextDocSettings::setTabStopWidth(pageEditor_->ui.tabStopWidthSpin->value());

	pageCharsets_->applySettings();

	emit applied();
}

void SettingsDlg::ok() {
	apply();
	accept();
}
